#ifndef VTreeNode_H
#define VTreeNode_H

#include "VItem.h"

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

class VTreeNode
{
	V_COUNTER_OBJECTS(VTreeNode)

public:
	VTreeNode();
	~VTreeNode();

	void deleteAllChildren();

	VTreeNode *parent() { return m_parent;}
	
	void setHasChildren(bool v)	{ m_hasChildren = v;}
	bool hasChildren() const { return m_hasChildren;}

	int childCount() const { return m_children.count();}
	//void appendChild(VTreeNode *child);
	void insertChild(int row,VTreeNode *child);
	void moveNode(VTreeNode *move,int toRow);
	void setChildren(const QList<VTreeNode*> &children);
	void deleteChild(int row);
	VTreeNode *child(int row) { return m_children[row];}
	int row() const;

	void setItem(VItem *item) { delete m_item; m_item = item;}
	VItem *item() { return m_item;}

	VTreeNode *findNodeByIdNav(int idNav);
	
public:
	static VTreeNode *init();
	
private:
	VTreeNode *m_parent;
	bool m_hasChildren;
	QList<VTreeNode*> m_children;
	VItem *m_item;
};

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

class VDataRecord
{
	V_COUNTER_OBJECTS(VDataRecord)

public:
	VDataRecord() :m_idRec(0) {}
	~VDataRecord() { deleteAllDataFields();}

	void setIdRec(int v) { m_idRec = v;}
	int idRec() const { return m_idRec;}

	void deleteAllDataFields() { qDeleteAll(m_dataFields); m_dataFields.clear();}

	void dataFieldsFactory(VTreeNode *node);
	int dataFieldCount() const { return m_dataFields.count();}
	VDataField *dataField(int column) const { return m_dataFields[column];}

	VDataRecord *clone() const;
	
private:
	int m_idRec;
	QList<VDataField*> m_dataFields;
};

///////////////////////////////////////////////////////////////////////////////

class VDataTable
{
	V_COUNTER_OBJECTS(VDataTable)

public:
	VDataTable();
	~VDataTable() { deleteAllRecords();}

	void setNode(VTreeNode *node = NULL);
	VTreeNode *node() const { return m_node;}
	
	int idTable() const;

	void deleteAllRecords() { qDeleteAll(m_dataRecords); m_dataRecords.clear();}

	int dataFieldCount() const { return m_node ? m_node->childCount() : 0;}

	int dataRecordCount() const { return m_dataRecords.count();}
	void appendDataRecord(VDataRecord *record) { m_dataRecords.append(record);}
	void insertDataRecord(int row,const VDataRecord &record);
	void insertDataRecord(int row,VDataRecord *record);
	void insertDataRecord(int row,int idRec);
	void setDataRecord(const QList<VDataRecord*> &records);
	void deleteDataRecord(int row);
	VDataRecord *dataRecord(int row) const { return m_dataRecords[row];}
	int dataRecordRow(VDataRecord *record) const;

private:
	VTreeNode *m_node;
	QList<VDataRecord*> m_dataRecords;
};

///////////////////////////////////////////////////////////////////////////////

#endif
